﻿using System.Data;
using Loki.Data;
using Luna.Data.Mappers;
using Luna.Model.Accounting;

namespace Luna.Data.Accounting.Mappers
{
    public class AccountOperationDataMapper : LunaEntityMapper<AccountOperationData, IAccountOperationData>
    {
        public const string COL_ACCOUNT = "AccountID";
        public const string COL_AMOUNT = "Amount";
        public const string COL_CATEGORY = "CategoryID";
        public const string COL_PROJECT = "ProjectID";
        public const string COL_DESCRIPTION = "Description";
        public const string COL_EXECUTION_DATE = "ExecutionDate";
        public const string COL_OPERATION_TYPE = "OperationType";
        public const string COL_VALUE_DATE = "ValueDate";

        protected override void LoadCoreData(IAccountOperationData P_Entity, IDataRecord P_Record)
        {
            P_Entity.AccountID = P_Record.GetGuid(COL_ACCOUNT);
            P_Entity.Amount = P_Record.GetDecimal(COL_AMOUNT);
            P_Entity.CategoryID = P_Record.GetGuid(COL_CATEGORY);
            P_Entity.ProjectID = P_Record.GetNullableGuid(COL_PROJECT);
            P_Entity.Description = P_Record.GetString(COL_DESCRIPTION);
            P_Entity.ExecutionDate = P_Record.GetDate(COL_EXECUTION_DATE);
            P_Entity.OperationType = P_Record.GetEnum<AccountOperationType>(COL_OPERATION_TYPE);
            P_Entity.ValueDate = P_Record.GetNullableDate(COL_VALUE_DATE);
        }

        protected override void MapSaveParameters(IDatabaseCommand P_Command, IAccountOperationData P_Entity)
        {
            P_Command.AddParameter(COL_ACCOUNT, P_Entity.AccountID);
            P_Command.AddParameter(COL_CATEGORY, P_Entity.CategoryID);
            P_Command.AddParameter(COL_PROJECT, P_Entity.ProjectID);
            P_Command.AddParameter(COL_AMOUNT, P_Entity.Amount);
            P_Command.AddParameter(COL_DESCRIPTION, P_Entity.Description);
            P_Command.AddParameter(COL_EXECUTION_DATE, P_Entity.ExecutionDate);
            P_Command.AddParameter(COL_VALUE_DATE, P_Entity.ValueDate);
            P_Command.AddParameter(COL_OPERATION_TYPE, P_Entity.OperationType.ToString());
            P_Command.AddParameter(COL_ID, P_Entity.PK);
        }
    }
}
